package project1.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import project1.model.User;
import project1.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class UserDaoIml implements UserDao {
    @Override
    public List<User> allUser() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> allUser =null ;
        try {
            allUser = runner.query("select * from user", new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return allUser;
    }

    @Override
    public void deleteUser(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from user where id = ? ",id );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<User> searchUser(String word) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<User> userList = null;
        try {
             userList = runner.query("select * from user where nickname like ? ", new BeanListHandler<User>(User.class), word);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
}
